<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :page.sync="page"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import { mapGetters } from 'vuex';
import { getOptionLogs } from '@/api/logs';

export default {
  name: 'operationLog',
  data() {
    return {
      form: {},
      selectionList: [],
      query: {
        createTimeStart: this.getDefaultTime()[0],
        createTimeEnd: this.getDefaultTime()[1]
      },
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'calc(100vh - 450px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        viewText: '详情',
        viewBtnText: '详情',
        editBtn: false,
        addBtn: false,
        delBtn: false,
        menuWidth: 120,
        // dialogType: 'drawer',
        column: [
          {
            label: '操作模块',
            prop: 'module'
          },
          {
            label: '日志类型',
            prop: 'title',
            type: 'select',
            dicUrl: '/api/mes-admin/operationLog/getOperationLogTitleDict',
            searchSpan: 4,
            props: {
              label: 'value',
              value: 'code'
            },
            search: true
          },
          {
            label: '操作对象',
            searchSpan: 4,
            prop: 'objCode',
            search: true
          },
          {
            label: '备注',
            prop: 'remark',
            searchSpan: 4,
            search: true
          },
          {
            label: '操作人',
            prop: 'createBy'
          },
          {
            label: '操作时间',
            prop: 'createTime'
          },
          {
            label: '操作时间',
            prop: 'time',
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            hide: true,
            searchSpan: 6,
            viewDisplay: false,
            searchValue: this.getDefaultTime(),
            searchRules: [
              {
                trigger: 'change',
                validator: (rule, value, callback) => {
                  const endTime = value[1];
                  const startTime = value[0];
                  if (this.$moment(endTime).diff(this.$moment(startTime), 'day') > 92) {
                    callback(new Error('查询时间最多选择三个月时间'));
                  }
                  callback();
                }
              }
            ],
            search: true
          },
          {
            label: '操作ip',
            prop: 'remoteIp'
          },
          {
            label: '请求地址',
            prop: 'requestUri',
            hide: true,
            viewDisplay: true
          },
          {
            label: '请求参数',
            prop: 'params',
            type: 'textarea',
            hide: true,
            viewDisplay: true
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.operation_log_view, false)
      };
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    getDefaultTime() {
      const startTime = this.$moment(new Date()).subtract(7, 'days').format('yyyy-MM-DD hh:mm:ss');
      const endTime = this.$moment(new Date()).format('yyyy-MM-DD hh:mm:ss');
      return [startTime, endTime];
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.time && params.time.length > 0) {
        params.createTimeStart = params.time[0];
        params.createTimeEnd = params.time[1];
        delete params.time;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    beforeOpen(done, type) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getOptionLogs(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>

<style scoped></style>
